STM32超低功耗入门之睡眠模式 您所在的位置:网站首页 low 怎么写 STM32超低功耗入门之睡眠模式

STM32超低功耗入门之睡眠模式

2024-07-17 00:37| 来源: 网络整理| 查看: 265

一. 认识睡眠模式

查看官方手册对睡眠模式的描述: 在这里插入图片描述 在这里插入图片描述

通过上图可以得出结论:

睡眠模式有 4 种电压调节器方案在睡眠模式下 CPU 是停止状态在睡眠模式下程序在 SRAM 执行情况下,Flash 可以被断电SRAM1 SRAM2 可以独立的开启或关闭时钟都处于开启状态, 低功耗运行模式下 PLL 不能使用根据电压调节器的选择,外设全部开启或者 USB_FS RNG 不能使用所有的中断和事件都可以唤醒根据选择的电压调节器,MCU 也有 4 种功耗,功耗与运行的频率成正比关系唤醒时间 6 个时钟周期 SMPS (Switched-mode Power Supply) 二. 睡眠模式的进入

进入睡眠模式的方法很简单,直接调用 void HAL_PWR_EnterSLEEPMode(uint32_t Regulator, uint8_t SLEEPEntry) 使用这个函数时,先看看 ST 的注释

/** * @brief Enter Sleep or Low-power Sleep mode. * @note In Sleep/Low-power Sleep mode, all I/O pins keep the same state as in Run mode. * @param Regulator: Specifies the regulator state in Sleep/Low-power Sleep mode. * This parameter can be one of the following values: * @arg @ref PWR_MAINREGULATOR_ON Sleep mode (regulator in main mode) * @arg @ref PWR_LOWPOWERREGULATOR_ON Low-power Sleep mode (regulator in low-power mode) * @note Low-power Sleep mode is entered from Low-power Run mode. Therefore, if not yet * in Low-power Run mode before calling HAL_PWR_EnterSLEEPMode() with Regulator set * to PWR_LOWPOWERREGULATOR_ON, the user can optionally configure the * Flash in power-down monde in setting the SLEEP_PD bit in FLASH_ACR register. * Additionally, the clock frequency must be reduced below 2 MHz. * Setting SLEEP_PD in FLASH_ACR then appropriately reducing the clock frequency must * be done before calling HAL_PWR_EnterSLEEPMode() API. * @note When exiting Low-power Sleep mode, the MCU is in Low-power Run mode. To move in * Run mode, the user must resort to HAL_PWREx_DisableLowPowerRunMode() API. * @param SLEEPEntry: Specifies if Sleep mode is entered with WFI or WFE instruction. * This parameter can be one of the following values: * @arg @ref PWR_SLEEPENTRY_WFI enter Sleep or Low-power Sleep mode with WFI instruction * @arg @ref PWR_SLEEPENTRY_WFE enter Sleep or Low-power Sleep mode with WFE instruction * @note When WFI entry is used, tick interrupt have to be disabled if not desired as * the interrupt wake up source. * @retval None */ 这个函数时让 MCU 进入睡眠模式或者低功耗睡眠模式睡眠模式和低功耗睡眠模式,所有的 I/O 的状态都和运行模式时是一样的Regulator 可以传入的参数有 2 个。 PWR_MAINREGULATOR_ON 调节器在主模式。PWR_LOWPOWERREGULATOR_ON 调节器在低功耗模式从低功耗运行模式切换到低功耗睡眠模式。如果设置调节器在为 LPR 之前,系统时钟要降低到 2MHZ以下当退出低功耗睡眠模式后,MCU 会进入低功耗运行模式,如果要切换到运行模式,则需要通过HAL_PWREx_DisableLowPowerRunMode() 来退出低功耗运行模式进入睡眠模式的方式。WFI 还是 WFE当使用 WFI 进入睡眠模式前,必须要关闭 tick 的中断,不然 tick 的中断会唤醒 MCU 三. 睡眠模式的进入代码

进入睡眠模式的代码实现

int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); HAL_Delay(500); // 上电之后延时一会再进入睡眠模式,这样可以保证复位之后可以立即下载程序 HAL_SuspendTick(); // 关闭 tick 中断,防止唤醒睡眠状态的 MCU HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI); HAL_ResumeTick(); while (1) { HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_13); HAL_Delay(500); } }

代码解析:

进入睡眠模式后将导致调试器无法识别到 MCU,所以在启动的时候加了一个 HAL_Delay(500) ,如果没有加,则需要按住复位键点击下载,点击下载之后立即松开复位键,就可以正常的下载程序了。HAL 生成的代码默认将 systick 的中断设置为 1KHZ.进入睡眠模式前需要关闭 tick 中断,防止唤醒睡眠状态的 MCU让 MCU 进入睡眠模式 HAL_PWR_EnterSLEEPMode唤醒 MCU 之后,将执行进入睡眠模式的下一个指令。所以要立刻打开 tick 中断 在这里插入图片描述 通过这个表可以看到,睡眠模式支持 WFI 和 WFE WFI: 立刻进入低功耗模式 WFE: 不是立刻进入低功耗模式,根据Event Register(一个单bit的寄存器,每个PE一个)的状态,有两种情况:如果Event Register为1,该指令会把它清零,然后执行完成(不会standby) 选择 WFE 可是使用如下代码 HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFE); 四. 总结 进入睡眠模式是 MCU 停止,唤醒之后无需重新配置时钟睡眠模式唤醒后,无需对外设重新进入初始化睡眠模式支持 中断唤醒 和 事件唤醒USB_FS RNG 不能使用


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有